/***************************************************************
*  Copyright (c) 2007 by GroupMe! Team (www.groupme.net)
*  All rights reserved.
*
*  This file is part of the GroupMe! Project. Source code of 
*  this project is closed and redistribution of this code is
*  prohibited. 
*  
*  Contact: http://www.groupme.net
*
*  This copyright notice MUST APPEAR in all copies of the file!
***************************************************************/
package net.groupme.utils;

import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;


/**
 * created on Jul 3, 2007
 * 
 * This class provides some helper functions that operate on strings. For example
 * it provides a method which converts a given query string into an Array of
 * Tags...
 * 
 * @author Fabian Abel, <a href="mailto:abel@l3s.de">abel@l3s.de</a>
 * @author last edited by: $Author: fabian $
 * 
 * @version $Revision: 1.5 $ $Date: 2007-12-17 19:45:15 $
 */
public class StringUtilities {

	/**
	 * Extracts single tag keywords from a given string. Example:
	 * <pre>
	 * given string:  Ajax "Semantic Web" "Web 2.0" design
	 * resulting list: [Ajax, Semantic Web, Web 2.0, design]
	 * </pre>
	 * @param keywords the keword query string
	 * @param optimizeForSearch should be <code>true</code> if query string should be optimized, e.g. add "Semantic Web" if semantic web (= two tags) are given.
	 * @return a collection of seperated keywords
	 */
	public static final Collection<String> extractTags(String keywords, boolean optimizeForSearch){
		Collection<String> tags = new ArrayList<String>();
		if(keywords == null || "".equals(keywords.trim())){
			return tags;
		}
		keywords = keywords.trim();
		
		//small workaround (Problem: Search String = semantic web -> Stuff that is tagged with "semantic web" is not found.)
		if(optimizeForSearch && keywords.contains(" ") && !keywords.contains("\"")){
			keywords += " \"" + keywords + "\"";
		}
		
		while(keywords.length() > 0){
			if(keywords.contains(" ")){
				if(keywords.charAt(0) == '"'){// a keyword phrase begins (e.g. "Semantic Web")
					keywords = keywords.substring(1);
					if(keywords.contains("\"")){ //keyword phrase should end anywhere...
						tags.add(keywords.substring(0, keywords.indexOf("\"")));
						keywords = keywords.substring(keywords.indexOf("\"") + 1).trim();
					}
				}else{ //normal keyword:
					keywords = keywords.trim();
					if(keywords.contains(" ")){ //another keyword follows:
						tags.add(keywords.substring(0, keywords.indexOf(" ")));
						keywords = keywords.substring(keywords.indexOf(" ") + 1).trim();
					}else{//no keyword follows:
						tags.add(keywords);
						keywords = "";
						return tags;
					}
				}
			}else{//no keyword follows:
				if(keywords.contains("\"")){
					keywords = keywords.replaceAll("\"", "");
				}
				tags.add(keywords);
				keywords = "";
				return tags;
			}
		}
		return tags;
	}
	
	/**
	 * This method resolves the Id of an GroupMe! Object from an GroupMe! id.
	 * @param uri the URI of the GroupMe! Object
	 * @return the Id of te GroupMe! Object
	 */
	public static final String getIdFromURI(URI uri){
		String uriString = uri.toString();
		if (uriString.endsWith("/")){
			uriString = uriString.substring(0, uriString.length()-1);
		}
		return uriString.substring(uriString.lastIndexOf("/")+1);
	}
}
